
'''
########################################################
#程序文件ex15_6.py
import numpy as np

with open('data15_6.txt') as f:
    s = f.read().replace(' ','').replace('\n','')
a = np.zeros((4,4))  #统计数据初始化
mfind = lambda s,c: [x for x in range(s.find(c), len(s)) if s[x:x+2] == c]
for i in range(1,5):
    for j in range(1,5):
        a[i-1,j-1] = len(mfind(s,str(i)+str(j)))
print('统计数据矩阵a:\n', a) 
print('a的行和：',a.sum(axis=1))

########################################################
'''

########################################################
#by wang
import numpy as np
s = np.array([4, 3, 2, 1, 4, 3, 1, 1, 2, 3,
2, 1, 2, 3, 4, 4, 3, 3, 1, 1,
1, 3, 3, 2, 1, 2, 2, 2, 4, 4,
2, 3, 2, 3, 1, 1, 2, 4, 3, 1])

a = np.zeros((4,4))
for k in range(len(s)-1):
    a[s[k]-1,s[k+1]-1]+=1
print(a)

b=a.sum(axis=1)
print(b)

p = np.zeros((4,4))
for i in range(4):
    for j in range(4):
        p[i,j] = a[i,j]/b[i]
print(p)

